package com.hejie.videoservice.repository;

import com.hejie.videoservice.entity.Video;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Repository
public interface VideoRepository extends JpaRepository<Video, Long> {

    /**
     * 根据用户ID查询视频
     */
    Page<Video> findByUserIdAndDeletedFalse(Long userId, Pageable pageable);

    /**
     * 根据分类ID查询视频
     */
    Page<Video> findByCategoryIdAndStatusAndDeletedFalse(Long categoryId, Video.VideoStatus status, Pageable pageable);

    /**
     * 根据状态和可见性查询视频
     */
    Page<Video> findByStatusAndVisibilityAndDeletedFalse(Video.VideoStatus status, Video.Visibility visibility, Pageable pageable);

    /**
     * 搜索视频（标题或描述包含关键词）
     */
    @Query("SELECT v FROM Video v WHERE (v.title LIKE %:keyword% OR v.description LIKE %:keyword%) AND v.status = :status AND v.visibility = :visibility AND v.deleted = false")
    Page<Video> searchVideos(@Param("keyword") String keyword, @Param("status") Video.VideoStatus status, @Param("visibility") Video.Visibility visibility, Pageable pageable);

    /**
     * 获取热门视频（按观看次数排序）
     */
    @Query("SELECT v FROM Video v WHERE v.status = :status AND v.visibility = :visibility AND v.deleted = false ORDER BY v.viewCount DESC")
    Page<Video> findHotVideos(@Param("status") Video.VideoStatus status, @Param("visibility") Video.Visibility visibility, Pageable pageable);

    /**
     * 获取最新视频（按创建时间排序）
     */
    @Query("SELECT v FROM Video v WHERE v.status = :status AND v.visibility = :visibility AND v.deleted = false ORDER BY v.createTime DESC")
    Page<Video> findLatestVideos(@Param("status") Video.VideoStatus status, @Param("visibility") Video.Visibility visibility, Pageable pageable);

    /**
     * 批量查询视频
     */
    List<Video> findByIdInAndDeletedFalse(List<Long> ids);

    /**
     * 查询用户可见的视频（公开视频或用户自己的视频）
     */
    @Query("SELECT v FROM Video v WHERE (v.visibility = 'PUBLIC' OR v.userId = :userId) AND v.status = 'PUBLISHED' AND v.deleted = false")
    Page<Video> findVisibleVideos(@Param("userId") Long userId, Pageable pageable);

    /**
     * 更新视频观看次数
     */
    @Modifying
    @Transactional
    @Query("UPDATE Video v SET v.viewCount = v.viewCount + 1 WHERE v.id = :id AND v.deleted = false")
    int incrementViewCount(@Param("id") Long id);

    /**
     * 更新视频点赞数
     */
    @Modifying
    @Transactional
    @Query("UPDATE Video v SET v.likeCount = v.likeCount + :increment WHERE v.id = :id AND v.deleted = false")
    int updateLikeCount(@Param("id") Long id, @Param("increment") int increment);

    /**
     * 更新视频评论数
     */
    @Modifying
    @Transactional
    @Query("UPDATE Video v SET v.commentCount = v.commentCount + :increment WHERE v.id = :id AND v.deleted = false")
    int updateCommentCount(@Param("id") Long id, @Param("increment") int increment);

    /**
     * 更新视频收藏数
     */
    @Modifying
    @Transactional
    @Query("UPDATE Video v SET v.favoriteCount = v.favoriteCount + :increment WHERE v.id = :id AND v.deleted = false")
    int updateFavoriteCount(@Param("id") Long id, @Param("increment") int increment);

    /**
     * 根据视频ID逻辑删除视频
     */
    @Modifying
    @Transactional
    @Query("UPDATE Video v SET v.deleted = true WHERE v.id = :id")
    int logicallyDeleteVideo(@Param("id") Long id);

    /**
     * 根据用户ID逻辑删除所有视频
     */
    @Modifying
    @Transactional
    @Query("UPDATE Video v SET v.deleted = true WHERE v.userId = :userId")
    int logicallyDeleteVideosByUserId(@Param("userId") Long userId);
}